package com.bw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.domain.SysRoleMenu;
import com.bw.service.SysRoleMenuService;
import com.bw.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author THINKAPD
* @description 针对表【sys_role_menu】的数据库操作Service实现
* @createDate 2024-03-04 15:28:13
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService{
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public boolean edit(SysRoleMenu sysRoleMenu) {
        try {
            //1.根据角色ID 先删除中间表的数据
            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRoleMenu::getRoleId,sysRoleMenu.getRoleId());
            boolean b = this.remove(wrapper);
            //2.保存到中间表
            Integer[] mids = sysRoleMenu.getMids();
            if(mids!=null&& mids.length>0){
                for (Integer mid : mids) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(sysRoleMenu.getRoleId());
                    roleMenu.setMenuId(mid);
                    this.save(roleMenu);
                }
            }
            return true;
        }catch (Exception e){
          e.printStackTrace();
          throw  new RuntimeException("绑定失败");
        }

    }

    @Override
    public List<Integer> listMenuIdByRoleId(Integer roleId) {
        return sysRoleMenuMapper.listMenuIdByRoleId(roleId);
    }
}




